home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / file / logiso.000 / logiso / RCS / file.c,v < prev    next >
Encoding:
Text File  |  1995-03-24  |  7.3 KB  |  324 lines

  1. head    1.4;
  2. access;
  3. symbols
  4.     VER_0_3:1.4
  5.     VER_0_2:1.3;
  6. locks; strict;
  7. comment    @ * @;
  8.  
  9.  
  10. 1.4
  11. date    95.03.24.11.44.17;    author coulter;    state Exp;
  12. branches;
  13. next    1.3;
  14.  
  15. 1.3
  16. date    95.03.10.02.43.06;    author coulter;    state Exp;
  17. branches;
  18. next    1.2;
  19.  
  20. 1.2
  21. date    95.02.13.20.56.00;    author coulter;    state Exp;
  22. branches;
  23. next    1.1;
  24.  
  25. 1.1
  26. date    95.02.11.13.13.24;    author coulter;    state Exp;
  27. branches;
  28. next    ;
  29.  
  30.  
  31. desc
  32. @isofs regular file handling primitives.  Will add call to iso_ioctl.
  33. @
  34.  
  35.  
  36. 1.4
  37. log
  38. @Checkin version for 0.3 distribution.
  39. @
  40. text
  41. @/*
  42.  *  linux/fs/isofs/file.c
  43.  * 
  44.  *  Copyright 1995 Michael Coulter for access logging.
  45.  *
  46.  *  (C) 1992, 1993, 1994  Eric Youngdale Modified for ISO9660 filesystem.
  47.  *
  48.  *  (C) 1991  Linus Torvalds - minix filesystem
  49.  *
  50.  *  isofs regular file handling primitives
  51.  */
  52.  
  53. #include <asm/segment.h>
  54. #include <asm/system.h>
  55.  
  56. #include <linux/sched.h>
  57. #include <linux/iso_fs.h>
  58. #include <linux/fcntl.h>
  59. #include <linux/kernel.h>
  60. #include <linux/errno.h>
  61. #include <linux/stat.h>
  62. #include <linux/locks.h>
  63.  
  64. #include <linux/dirent.h>
  65.  
  66. #define    NBUF    32
  67.  
  68. #define MIN(a,b) (((a)<(b))?(a):(b))
  69. #define MAX(a,b) (((a)>(b))?(a):(b))
  70.  
  71. #include <linux/fs.h>
  72. #include <linux/iso_fs.h>
  73.  
  74. static int isofs_file_read(struct inode *, struct file *, char *, int);
  75.  
  76. /*
  77.  * We have mostly NULL's here: the current defaults are ok for
  78.  * the isofs filesystem.
  79.  */
  80. static struct file_operations isofs_file_operations = {
  81.     NULL,            /* lseek - default */
  82.     isofs_file_read,    /* read */
  83.     NULL,            /* write */
  84.     NULL,            /* readdir - bad */
  85.     NULL,            /* select - default */
  86.     iso_ioctl,        /* ioctl */
  87.     generic_mmap,        /* mmap */
  88.     NULL,            /* no special open is needed */
  89.     NULL,            /* release */
  90.     NULL            /* fsync */
  91. };
  92.  
  93. struct inode_operations isofs_file_inode_operations = {
  94.     &isofs_file_operations,    /* default file operations */
  95.     NULL,            /* create */
  96.     NULL,            /* lookup */
  97.     NULL,            /* link */
  98.     NULL,            /* unlink */
  99.     NULL,            /* symlink */
  100.     NULL,            /* mkdir */
  101.     NULL,            /* rmdir */
  102.     NULL,            /* mknod */
  103.     NULL,            /* rename */
  104.     NULL,            /* readlink */
  105.     NULL,            /* follow_link */
  106.     isofs_bmap,        /* bmap */
  107.     NULL,                   /* truncate */
  108.     NULL            /* permission */
  109. };
  110.  
  111. /* This is a heuristic to determine if a file is text of binary.  If it
  112.  * is text, then we translate all 0x0d characters to spaces.  If the 0x0d
  113.  * character is not preceded or followed by a 0x0a, then we turn it into
  114.  * a 0x0a.  A control-Z is also turned into a linefeed.
  115.  */
  116.  
  117. static inline void unixify_to_fs(char * outbuf, char * buffer, int chars,
  118.                  int mode)
  119. {
  120.     char outchar;
  121.  
  122.     while(chars--){
  123.           outchar = *buffer;
  124.         if(outchar == 0x1a) outchar = 0x0a;
  125.         if(outchar == 0x0d){
  126.             if(mode == ISOFS_FILE_TEXT_M) outchar = 0x0a;
  127.             if(mode == ISOFS_FILE_TEXT) outchar = ' ';
  128.         }
  129.         put_fs_byte(outchar, outbuf++);
  130.         buffer++;
  131.     }
  132. }
  133.  
  134. /*This function determines if a given file has a DOS-like text format or not*/
  135.  
  136. static void isofs_determine_filetype(struct inode * inode)
  137. {
  138.     int block;
  139.     int result, i;
  140.     struct buffer_head * bh;
  141.     unsigned char * pnt;
  142.     
  143.     block = isofs_bmap(inode,0);
  144.     if (block && (bh = bread(inode->i_dev,block, ISOFS_BUFFER_SIZE(inode)))) {
  145.         pnt = (unsigned char *) bh->b_data;
  146.         result = ISOFS_FILE_TEXT_M;
  147.         for(i=0;i<(inode->i_size < ISOFS_BUFFER_SIZE(inode) ? inode->i_size : ISOFS_BUFFER_SIZE(inode));
  148.             i++,pnt++){
  149.             if(*pnt & 0x80) {result = ISOFS_FILE_BINARY; break;};
  150.             if(*pnt >= 0x20 || *pnt == 0x1a) continue;
  151.             if(*pnt == 0x0a) {result = ISOFS_FILE_TEXT; continue;};
  152.             if(*pnt >= 0x9 && *pnt <= 0x0d) continue;
  153.             result = ISOFS_FILE_BINARY;
  154.             break;
  155.         }
  156.         brelse(bh);
  157.         inode->u.isofs_i.i_file_format = result;
  158.     }
  159. }
  160.  
  161. static int isofs_file_read(struct inode * inode, struct file * filp, char * buf, int count)
  162. {
  163.     int read,left,chars;
  164.     int block, blocks, offset;
  165.     int bhrequest;
  166.     int ra_blocks, max_block, nextblock;
  167.     struct buffer_head ** bhb, ** bhe;
  168.     struct buffer_head * bhreq[NBUF];
  169.     struct buffer_head * buflist[NBUF];
  170.     
  171.     iso_log_add(inode->i_ino, inode->i_dev, ISO_LOG_OP_isofs_file_read);
  172.     if (!inode) {
  173.         printk("isofs_file_read: inode = NULL\n");
  174.         return -EINVAL;
  175.     }
  176.     if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  177.         printk("isofs_file_read: mode = %07o\n",inode->i_mode);
  178.         return -EINVAL;
  179.     }
  180.     if (inode->u.isofs_i.i_file_format == ISOFS_FILE_UNKNOWN)
  181.         isofs_determine_filetype(inode);
  182.     if (filp->f_pos > inode->i_size)
  183.         left = 0;
  184.     else
  185.         left = inode->i_size - filp->f_pos;
  186.     if (left > count)
  187.         left = count;
  188.     if (left <= 0)
  189.         return 0;
  190.     read = 0;
  191.     block = filp->f_pos >> ISOFS_BUFFER_BITS(inode);
  192.     offset = filp->f_pos & (ISOFS_BUFFER_SIZE(inode)-1);
  193.     blocks = (left + offset + ISOFS_BUFFER_SIZE(inode) - 1) / ISOFS_BUFFER_SIZE(inode);
  194.     bhb = bhe = buflist;
  195.  
  196.     ra_blocks = read_ahead[MAJOR(inode->i_dev)] / (BLOCK_SIZE >> 9);
  197.     if(ra_blocks > blocks) blocks = ra_blocks;
  198.  
  199.     max_block = (inode->i_size + BLOCK_SIZE - 1)/BLOCK_SIZE;
  200.     nextblock = -1;
  201.  
  202.     /* We do this in a two stage process.  We first try and request
  203.        as many blocks as we can, then we wait for the first one to
  204.        complete, and then we try and wrap up as many as are actually
  205.        done.  This routine is rather generic, in that it can be used
  206.        in a filesystem by substituting the appropriate function in
  207.        for getblk.
  208.  
  209.        This routine is optimized to make maximum use of the various
  210.        buffers and caches. */
  211.  
  212.     do {
  213.             bhrequest = 0;
  214.         while (blocks) {
  215.                 int uptodate;
  216.             --blocks;
  217.             *bhb = getblk(inode->i_dev,isofs_bmap(inode, block++), ISOFS_BUFFER_SIZE(inode));
  218.             uptodate = 1;
  219.             if (*bhb && !(*bhb)->b_uptodate) {
  220.                     uptodate = 0;
  221.                     bhreq[bhrequest++] = *bhb;
  222.                   };
  223.  
  224.             if (++bhb == &buflist[NBUF])
  225.                 bhb = buflist;
  226.  
  227.             /* If the block we have on hand is uptodate, go ahead
  228.                and complete processing. */
  229.             if(uptodate) break;
  230.  
  231.             if (bhb == bhe)
  232.                 break;
  233.               }
  234.  
  235.         /* Now request them all */
  236.         if (bhrequest)
  237.           ll_rw_block(READ, bhrequest, bhreq);
  238.  
  239.         do{ /* Finish off all I/O that has actually completed */
  240.           if (*bhe) {/* test for valid buffer */
  241.             wait_on_buffer(*bhe);
  242.             if (!(*bhe)->b_uptodate) {
  243.               brelse(*bhe);
  244.               if (++bhe == &buflist[NBUF])
  245.             bhe = buflist;
  246.               left = 0;
  247.               break;
  248.             }
  249.           }
  250.           
  251.           if (left < ISOFS_BUFFER_SIZE(inode) - offset)
  252.             chars = left;
  253.           else
  254.             chars = ISOFS_BUFFER_SIZE(inode) - offset;
  255.           filp->f_pos += chars;
  256.           left -= chars;
  257.           read += chars;
  258.           if (*bhe) {
  259.             if (inode->u.isofs_i.i_file_format == ISOFS_FILE_TEXT ||
  260.             inode->u.isofs_i.i_file_format == ISOFS_FILE_TEXT_M)
  261.               unixify_to_fs(buf, offset+(*bhe)->b_data, chars, 
  262.                     inode->u.isofs_i.i_file_format);
  263.             else
  264.               memcpy_tofs(buf,offset+(*bhe)->b_data,chars);
  265.             brelse(*bhe);
  266.             buf += chars;
  267.           } else {
  268.             while (chars-->0)
  269.               put_fs_byte(0,buf++);
  270.           }
  271.           offset = 0;
  272.           if (++bhe == &buflist[NBUF])
  273.             bhe = buflist;
  274.         } while( bhe != bhb && (*bhe == 0 || !(*bhe)->b_lock) && 
  275.             (left > 0));
  276.     } while (left > 0);
  277.  
  278. /* Release the read-ahead blocks */
  279.     while (bhe != bhb) {
  280.       if (*bhe) brelse(*bhe);
  281.       if (++bhe == &buflist[NBUF])
  282.         bhe = buflist;
  283.     };
  284.  
  285.     filp->f_reada = 1;
  286.  
  287.     if (!read)
  288.         return -EIO;
  289.     return read;
  290. }
  291. @
  292.  
  293.  
  294. 1.3
  295. log
  296. @Checkin files modified to make version 0.2
  297. @
  298. text
  299. @d3 2
  300. @
  301.  
  302.  
  303. 1.2
  304. log
  305. @Checkin working version.  Try to make it dynamic now.
  306. @
  307. text
  308. @d129 1
  309. a129 1
  310.     iso_log_add(inode->i_ino, ISO_LOG_OP_isofs_file_read);
  311. @
  312.  
  313.  
  314. 1.1
  315. log
  316. @Initial revision
  317. @
  318. text
  319. @d44 1
  320. a44 1
  321.     NULL,            /* ioctl - default */
  322. d129 1
  323. @
  324.